package com.zhongying.jf.util;

import java.math.BigDecimal;

/**
 * Created by liyuxuan on 2016/7/10.
 */
public class RedEnvelopes {


    /**
     * 金额的精度：保留两位小数
     */
    private static int scale = 2;

    /**
     * 普通玩法
     * <p>
     * 输入金额、人数，随机输出金额列表，例如：<br>
     * 输入：1.0 3<br>
     * 输出：0.11 0.37 0.52
     * </p>
     *
     * @return
     */
    public static BigDecimal[] generalPlay(final BigDecimal money,
                                           int numberOfPeople) {
        // 检验参数的合法性
        checkGeneralPlayValidParam(money, numberOfPeople);

        // 将金额转化为分，就能转化为整数
        BigDecimal divisor = new BigDecimal(100);
        int n = money.multiply(divisor).intValue();
        // 从1--n之间随机抽出numberOfPeople个数。其实这里就是一个抽样问题
        BigDecimal[] result = new BigDecimal[numberOfPeople];
        int m = numberOfPeople;
        int index = 0;
        for (int i = 0; i < n; i++) {
            long bigrand = bigRand();
            if (bigrand % (n - i) < m) {
                result[index++] = new BigDecimal(i + 1).divide(divisor, scale,
                        BigDecimal.ROUND_HALF_UP);
                m--;
            }
        }
        // 分区间处理
        for (int i = numberOfPeople - 1; i > 0; i--) {
            if (i == (numberOfPeople - 1)) {
                // 最后一个就取剩余的
                result[i] = money.subtract(result[i - 1]);
            } else {
                result[i] = result[i].subtract(result[i - 1]);
            }
        }
        return result;
    }

    /**
     * 产生一个很大的随机整数
     *
     * @return
     */
    private static long bigRand() {
        long bigrand = (long) (Math.random() * Integer.MAX_VALUE)
                + Integer.MAX_VALUE;

        return bigrand;
    }

    /**
     * 检查方法{@link #generalPlay(BigDecimal, int)}参数的有效性
     */
    private static void checkGeneralPlayValidParam(final BigDecimal money,
                                                   int numberOfPeople) {
        // 确保人数大于等于1
        if (numberOfPeople < 1) {
            throw new RuntimeException("人数 " + numberOfPeople + " 应该大于0！");
        }
        // 确保每个人至少能分到0.01元
        if (money.compareTo(new BigDecimal("0.01").multiply(new BigDecimal(
                numberOfPeople))) < 0) {
            throw new RuntimeException("人数太多，钱不够分！");
        }
        // 确保money只有两位小数
        if (money.scale() > scale) {
            throw new RuntimeException("金额数据不对，最多保留两位小数！");
        }
    }


    //（100元,1元）（500元，3元）（1000元，6元）（3000元，10元）（5000元，15元）（10000元，20元）

    /**
     *
     当100<=X<500元，可使用红包总金额Y <=1元；
     当500<=X<1000元，可使用红包总金额Y <=3元；
     当1000<=X<3000元，可使用红包总金额Y <=6元；
     当3000<=X<5000元，可使用红包总金额Y <=10元；
     当5000<=X<10000元，可使用红包总金额Y <=15元；
     当10000<=X元，可使用红包总金额Y <=20元；

     * @param bv
     * @return
     */
    public static BigDecimal calcMin(float bv){
        BigDecimal bd = BigDecimal.ZERO;
        if(bv>0 && bv<=1){
            bd=new BigDecimal(100);
        }else if(bv > 1 && bv<=3){
            bd = new BigDecimal(500);
        }else if(bv>3 && bv<=6){
            bd = new BigDecimal(1000);
        }else if(bv>6 && bv<=10){
            bd = new BigDecimal(3000);
        }else if(bv>10 && bv<=15){
            bd = new BigDecimal(5000);
        }else if(bv>15){
            bd = new BigDecimal(10000);
        }
        return bd;
    }


    public static void main(String[] args) {
        /*BigDecimal[] bigDecimals = generalPlay(new BigDecimal(500), 1000);
        for(BigDecimal b : bigDecimals){
            System.out.println(b+":"+calcMin(b.floatValue()));
        }*/
        System.out.println("0.1:"+calcMin(0.1f));
        System.out.println("0.5:"+calcMin(0.5f));
        System.out.println("0.9:"+calcMin(0.9f));
        System.out.println("1.1:"+calcMin(1.1f));
        System.out.println("2.9:"+calcMin(2.9f));
        System.out.println("3:"+calcMin(3f));
        System.out.println("3.1:"+calcMin(3.1f));
        System.out.println("3.5:"+calcMin(3.5f));
        System.out.println("5.9:"+calcMin(5.9f));
        System.out.println("6:"+calcMin(6f));
        System.out.println("6.1:"+calcMin(6.1f));
        System.out.println("9.9:"+calcMin(9.9f));
        System.out.println("10:"+calcMin(10f));
        System.out.println("10.1:"+calcMin(10.1f));
        System.out.println("11.1:"+calcMin(11.1f));
        System.out.println("14.9:"+calcMin(14.9f));
        System.out.println("15:"+calcMin(15f));
        System.out.println("15.1:"+calcMin(15.1f));
        System.out.println("16.1:"+calcMin(16.1f));
        System.out.println("19.9:"+calcMin(19.9f));
        System.out.println("20:"+calcMin(20f));
        System.out.println("20.1:"+calcMin(20.1f));

    }
}
